package com.hsteach.framework.web.service;

import com.google.common.collect.Lists;
import com.hsteach.common.core.domain.model.AuthInfo;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.exception.XgkTokenException;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.framework.feign.ClientConstant;
import com.hsteach.framework.feign.client.auth.dto.AuthResp;
import com.hsteach.framework.feign.client.auth.dto.PageResp;
import com.hsteach.framework.feign.client.uapi.UApiClient;
import com.hsteach.framework.feign.client.uapi.vo.*;
import com.hsteach.framework.feign.client.xgk.dto.GetPersonByConditionEntity;
import com.hsteach.framework.feign.client.xgk.dto.XgkList;
import com.hsteach.framework.feign.client.xgk.dto.XgkListResult;
import com.hsteach.framework.feign.client.xgk.dto.XgkResult;
import com.hsteach.framework.feign.client.xgk.dto.ret.GetPersonByConditionRet;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UApiService {

    @Autowired
    private UApiClient uApiClient;

    private boolean isSuccess(AuthResp resp) {
        if (ClientConstant.AUTH_SUCCESS.equals(resp.getStatus())) {
            return true;
        }
        return false;
    }

    public List<Leader> getAllLeader(String accessToken, String appToken, String xxlType,String schoolId) {
        AuthResp<PageResp<Leader>> resp = uApiClient.pageLeader(accessToken, appToken, ClientConstant.AUTH_OPERATE_CODE_DEFAULT,xxlType, schoolId,1, 9999, 1);
        if (isSuccess(resp)) {
            if (resp.getResult().getCount() > 0) {
                return  resp.getResult().getList();
            } else {
                return Lists.newArrayList();
            }
        } else {
            throw new ServiceException("uapi api error: " + resp.getMessage());
        }
    }

    public List<DepartTeacher> getAllDepartTeacher(String accessToken, String appToken) {
        AuthResp<PageResp<DepartTeacher>> resp = uApiClient.pageTeacher(accessToken, appToken, ClientConstant.AUTH_DEPART_CODE_DEFAULT, 1, 1, 9999);
        if (isSuccess(resp)) {
            if (resp.getResult().getCount() > 0) {
                return  resp.getResult().getList();
            } else {
                return Lists.newArrayList();
            }
        } else {
            throw new ServiceException("uapi api error: " + resp.getMessage());
        }
    }
    public List<Company> getCompanyList(String accessToken, String appToken, String searchWord, String organizeId, Integer type) {
        AuthResp<PageResp<Company>> resp = uApiClient.pageCompany(accessToken, appToken, ClientConstant.AUTH_OPERATE_CODE_VALUE, 1, searchWord, organizeId, type, 1, 9999);
        if (isSuccess(resp)) {
            if (resp.getResult().getCount() > 0) {
                return  resp.getResult().getList();
            } else {
                return Lists.newArrayList();
            }
        } else {
            throw new ServiceException("uapi api error: " + resp.getMessage());
        }
    }

    public List<CompanyAndDept> getCompanyAndDeptList(String accessToken, String appToken) {
        XgkListResult<CompanyAndDept> resp = uApiClient.getCompanyAndDeptList(accessToken, appToken, ClientConstant.AUTH_OPERATE_CODE_VALUE, 1);
        if(resp == null) {
            return new ArrayList<>();
        }
        if (isSuccess(resp)) {
            if (resp.getResult().size() > 0) {
                return  resp.getResult();
            } else {
                return Lists.newArrayList();
            }
        } else {
            throw new ServiceException("uapi api error: " + resp.getMessage());
        }
    }

    /**
     * 条件查询人员列表
     * @param entity
     * @return
     */
    public List<GetPersonByConditionRet> getPersonByCondition(GetPersonByConditionEntity entity) {
        AuthInfo authInfo = SecurityUtils.getAuthInfo();
        String accessToken = authInfo.getAccessToken();
        String appToken = authInfo.getAppToken();
        //TODO operateCode似乎没有用，这里随便写的
        String operateCode = "0";
        XgkResult<XgkList<GetPersonByConditionRet>> resp = uApiClient.getPersonByCondition(accessToken,appToken,operateCode,
                entity.getRoleId(),entity.getName(),entity.getNickName(),
                entity.getOrganizeId(),entity.getCurPageNumber(),entity.getPageSize());
        if (isSuccess2(resp)) {
            if (ObjectUtils.isNotEmpty(resp.getResult())) {
                return resp.getResult().getList();
            } else {
                return Lists.newArrayList();
            }
        } else {
            throw new ServiceException("uapi api error: " + resp.getMessage());
        }
    }

    /**
     * 条件查询人员列表
     * @param
     * @return
     */
    public List<GetPersonByConditionRet> getMaintenancePerson(String appToken,String accessToken) {
        //TODO operateCode似乎没有用，这里随便写的
        String operateCode = "sbwx_";
        XgkResult<XgkList<GetPersonByConditionRet>> resp = uApiClient.getPersonByCondition(accessToken,appToken,operateCode,
                "571279888811864064",null,null,
                null,"1","999");
        if (isSuccess2(resp)) {
            if (ObjectUtils.isNotEmpty(resp.getResult())) {
                return resp.getResult().getList();
            } else {
                return Lists.newArrayList();
            }
        } else {
            throw new ServiceException("uapi api error: " + resp.getMessage());
        }
    }

    private boolean isSuccess(XgkListResult resp) {
        if (ObjectUtils.isEmpty(resp)) {
            return false;
        }
        if (StringUtils.isNotEmpty(resp.getMessage()) && resp.getMessage().indexOf("用户令牌己更新") != -1) {
            throw new XgkTokenException(resp.getMessage());
        }
        if (ClientConstant.AUTH_SUCCESS.equals(resp.getStatus())) {
            return true;
        }
        if (ClientConstant.XGK_SUCCESS.equals(resp.getCode())) {
            return true;
        }
        if (ClientConstant.XGK_SUCCESS.equals(resp.getStatus())) {
            return true;
        }
        return false;
    }
    private boolean isSuccess2(XgkResult resp) {
        if (ObjectUtils.isEmpty(resp)) {
            return false;
        }
        if (StringUtils.isNotEmpty(resp.getMessage()) && resp.getMessage().indexOf("用户令牌己更新") != -1) {
            throw new XgkTokenException(resp.getMessage());
        }
        if (ClientConstant.XGK_SUCCESS.equals(resp.getStatus())) {
            return true;
        }
        return false;
    }

    public List<SchoolListVO> getAllSchool(String accessToken, String appToken) {
        AuthResp<List<SchoolListVO>> resp = uApiClient.schoolList(accessToken, appToken, ClientConstant.AUTH_OPERATE_CODE_DEFAULT,"1");
        if (isSuccess(resp)) {
            return resp.getResult();
        } else {
            throw new ServiceException("uapi api error: " + resp.getMessage());
        }
    }
}
